package com.xth8013.config;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * description:
 *
 * @author tianhua.xie（tianhua.xie@ucarinc.com）
 * @version 1.0
 * @date 2018/8/20 8:27
 */
public class Config {

    private static InputStream configStream = null;
    private static Properties properties = new Properties();

    public static String date;
    public static String fromStation;
    public static String toStation;
    public static String purposeCodes;
    public static List<String> stationTrainCodeList;
    public static boolean needEd;
    public static boolean needYd;
    public static boolean needYz;


    static {
        needYz = false;
        needYd = false;
        needEd = false;
        configStream = Config.class.getClassLoader().getResourceAsStream("application.properties");
        try {
            //加上utf-8,处理乱码问题
            properties.load(new InputStreamReader(configStream,"utf-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        date = properties.getProperty("date");
        fromStation = properties.getProperty("fromStation");
        toStation = properties.getProperty("toStation");
        purposeCodes = properties.getProperty("purposeCodes", "ADULT");

        String codeList = properties.getProperty("stationTrainCodeList");
        if (codeList!=null&&codeList.trim().length()>0){
            stationTrainCodeList = Arrays.asList(codeList.split(","));
        }


        needEd = "true".equalsIgnoreCase(properties.getProperty("needEd"));
        needYd = "true".equalsIgnoreCase(properties.getProperty("needYd"));
        needYz = "true".equalsIgnoreCase(properties.getProperty("needYz"));

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-dd-MM");
        String nowDate = simpleDateFormat.format(new Date());
        if(date==null||date.trim().length()<=0){
            date = nowDate;
        }

    }

    public static void printConfig(){
        StringBuffer sb = new StringBuffer();
        sb.append("日期：").append(date).append(" ,")
                .append("出发点：").append(fromStation).append(" ,")
                .append("目的地：").append(toStation).append(" ,")
                .append("乘客类别：").append(purposeCodes).append(" ,")
                .append("车次列表：").append(stationTrainCodeList).append(" ,")
                .append("一等座：").append(needYd).append(" ,")
                .append("二等座：").append(needEd).append(" ,")
                .append("无座：").append(needYz);
        System.out.println(sb.toString());
    }
}
